<template>
	<view class="content">
		<view class="help-pubuBox">
			<u-search placeholder="搜索人员姓名" :clearabled="true" height="40" v-model="title" @search="chaxun" color="#2B2B2B"
				@custom="$dianji(cancel,'onoff')" shape="square" :showAction="showAction" :actionText="'取消'"
				:inputStyle="inputStyle" @change="onChange" @clear="cancel" :actionStyle="actionStyle"></u-search>
		</view>
		<view class="user-box" v-if="!isSearch">
			<m-tree ref="mtree" :defaultProps="defaultProps" :isGroup='true' :isUser="true" :data="tree" :divider="true"
				:unfold="true" @node-click="nodeClick">
			</m-tree>
		</view>
		<view class="search-box" v-else>
			<view class="main-box" v-for="item in searchUserTree" :key="item.id" @click="handleUser(item)">
				<image src="@/static/images/index/user.png" class="edit-icon" />
				{{item.deptName}}-{{item.name}}
			</view>
		</view>
		<view class="empty-data" v-if="tree.length==0 || (isSearch && searchUserTree.length==0)">
			<u-empty mode="data" :marginTop="100"></u-empty>
		</view>
		<view class="bottom-subside"></view>
		<view class="kiss-bottom-double">
			<view class="btn-left" @click="$dianji(cancel,'onoff1')">取消</view>
			<view class="btn-right" @click="$dianji(sumbit,'onoff2')">确定</view>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				onoff: true,
				//搜索值
				title: '',
				//按钮样式
				actionStyle: {
					color: '#0F53AC',
					fontSize: "32rpx",
				},
				inputStyle: {
					fontSize: '32rpx',
				},
				onoff2: true,
				onoff1: true,
				defaultProps: {
					id: 'id', // 此项为id项的key
					children: 'children', // 此项为修改子集数据的key
					label: 'name' // 此项为修改显示数据的key
				},
				tree: [],
				allUserTree: [],
				searchUserTree: [],
				isSearch: false,
				showAction: false,
				directorId: ''
			}
		},
		onLoad(options) {
			this.getUnitData()
			this.getUserData()
			if (options) {
				this.directorId = options.directorId
			}
		},
		methods: {
			getUnitData() {
				uni.$u.http.post(`/base/mobile/sysDeptInfo/getDeptUserTreeList`, []).then(res => {
					if (res.code == 0) {
						let newData = this.format(res.data)
						this.tree = newData
						if (this.directorId) {
							this.directorId.split(',').map(item => {
								this.setStatus(item, this.tree)
							})
						}
					}
				}).catch(err => {})
			},
			getUserData() {
				uni.$u.http.post(`/base/mobile/userInfo/getUserInfoList`, []).then(res => {
					if (res.code == 0) {
						this.allUserTree = res.data
						this.searchUserTree = res.data
					}
				}).catch(err => {})
			},
			format(arr) {
				arr.map(item => {
					item.checked = false
					if (!item.children) {
						item.children = []
					} else if (item.children) {
						item.children = this.format(item.children)
					}
				})
				return arr
			},
			setStatus(e, arr) {
				arr.map(item => {
					if (!e.id) {
						if (item.id == e) {
							item.checked = !item.checked
						}
						if (item.children.length) {
							item.children = this.setStatus(e, item.children)
						}
					} else {
						if (item.id == e.id) {
							item.checked = !item.checked
						}
						if (item.children.length) {
							item.children = this.setStatus(e, item.children)
						}
					}
				})
				return arr
			},
			nodeClick(e) {
				// 多选
				if (!e.children.length && e.type == 2) {
					this.setStatus(e, this.tree)
				}
				// 单选
				// if (!e.children.length && e.type == 2) {
				// 	uni.$emit('setUnit', e)
				// 	uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				// 		delta: 1
				// 	});
				// }
			},
			cancel() {
				this.title = ''
				this.isSearch = false
				this.showAction = false
			},
			sumbit() {
				let arr = this.checked(this.tree, [])
				if (!arr.length) return uni.$u.toast("人员选择不能为空")
				if(arr.length>10) return uni.$u.toast("最多选择十个人")
				uni.$emit('setUser', arr)
				uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			},
			checked(arr, newArr) {
				arr.map(item => {
					if (item.checked) {
						newArr.push(item)
					}
					if (item.children.length) {
						this.checked(item.children, newArr)
					}
				})
				return newArr
			},
			onChange(e) {
				if (e) {
					this.isSearch = true
					this.showAction = true
					this.searchUserTree = this.allUserTree.filter(item => item.name.indexOf(e) != -1)
				}
			},
			handleUser(item) {
				this.setStatus(item, this.tree)
				this.cancel()
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #ededed;
	}

	.help-pubuBox {
		padding: 22rpx;
		background: #fff;
	}

	.user-box {
		background-color: #fff;
		padding-bottom: 50rpx;
	}

	.search-box {
		background-color: #fff;
		padding: 0 22rpx;

		.main-box {
			display: flex;
			align-items: center;
			border-bottom: 1px solid #f2f2f2;
			font-size: 30rpx;
			padding: 16rpx 50rpx;

			.edit-icon {
				width: 40rpx;
				height: 40rpx;
				padding-right: 10rpx;
			}
		}
	}
</style>
